//
//  interitance_exercise.cpp
//  learnC++
//
//  Created by liuzhibing on 2018/6/13.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#include "interitance_exercise.hpp"
Monster::MonsterData Monster::monsterData[Monster::MAX_TYPES]
{
    { "dragon", 'D', 20, 4, 100 },
    { "orc", 'o', 6, 2, 25 },
    { "slime", 's', 1, 1, 5 }
};


// This function handles the player attacking the monster
void attackMonster(Player &p, Monster &m)
{
    // If the player is dead, we can't attack the monster
    if (p.isDead())
        return;
    
    std::cout << "You hit the " << m.getName() << " for " << p.getDamage() << " damage.\n";
    
    // Reduce the monster's health by the player's damage
    m.reduceHealth(p.getDamage());
    
    // If the monster is now dead, level the player up
    if (m.isDead())
    {
        std::cout << "You killed the " << m.getName() << ".\n";
        p.levelUp();
        std::cout << "You are now level " << p.getLevel() << ".\n";
        std::cout << "You found " << m.getGold() << " gold.\n";
        p.addGold(m.getGold());
    }
}

// This function handles the monster attacking the player
void attackPlayer(Monster &m, Player &p)
{
    // If the monster is dead, it can't attack the player
    if (m.isDead())
        return;
    
    // Reduce the player's health by the monster's damage
    p.reduceHealth(m.getDamage());
    std::cout << "The " << m.getName() << " hit you for " << m.getDamage() << " damage.\n";
}

// This function handles the entire fight between a player and a randomly generated monster
void fightMonster(Player &p)
{
    // First randomly generate a monster
    Monster m = Monster::getRandomMonster();
    std::cout << "You have encountered a " << m.getName() << " (" << m.getSymbol() << ").\n";
    
    // While the monster isn't dead and the player isn't dead, the fight continues
    while (!m.isDead() && !p.isDead())
    {
        std::cout << "(R)un or (F)ight: ";
        char input;
        std::cin >> input;
        if (input == 'R' || input == 'r')
        {
            // 50% chance of fleeing successfully
            if (getRandomNumber(1, 2) == 1)
            {
                std::cout << "You successfully fled.\n";
                return; // success ends the encounter
            }
            else
            {
                // Failure to flee gives the monster a free attack on the player
                std::cout << "You failed to flee.\n";
                attackPlayer(m, p);
                continue;
            }
        }
        
        if (input == 'F' || input == 'f')
        {
            // Player attacks first, monster attacks second
            attackMonster(p, m);
            attackPlayer(m, p);
        }
    }
}
// Generate a random number between min and max (inclusive)
// Assumes srand() has already been called
int getRandomNumber(int min, int max)
{
    static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);  // static used for efficiency, so we only calculate this value once
    // evenly distribute the random number across our range
    return static_cast<int>(rand() * fraction * (max - min + 1) + min);
}
